package com.zmx.doctor.view.activity;


import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.haibin.calendarview.Calendar;
import com.haibin.calendarview.CalendarView;
import com.zmx.doctor.BaseActivity;
import com.zmx.doctor.R;
import com.zmx.doctor.adapter.TripAdapter;
import com.zmx.doctor.fragment.FourFragment;
import com.zmx.doctor.okhttp.OkHttp3ClientManager;
import com.zmx.doctor.pojo.Trip;
import com.zmx.doctor.utils.AllUtil;
import com.zmx.doctor.view.util.MySharedPreferences;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TripActivity extends BaseActivity {

    private CalendarView gv;
    private TextView no_data;
    private Toolbar toolbar;
    private ImageButton add_button;
    private RecyclerView reView;

    private List<Trip> lists;
    private TripAdapter adapter;

    public SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");

    private String choose_date;//选择时间

    @Override
    protected int getLayoutId() {
        return R.layout.activity_trip;
    }

    @Override
    protected void initViews() {

        toolbar = findViewById(R.id.tool_bar);
        setToolbar(R.id.tool_bar);
        gv = findViewById(R.id.calendarView);
        toolbar.setTitle(format.format(new Date()));
        no_data = findViewById(R.id.no_data);
        choose_date = sdFormatter.format(new Date());

        reView = findViewById(R.id.reView);
        reView.setLayoutManager(new LinearLayoutManager(this));
        lists = new ArrayList<>();
        adapter = new TripAdapter(this);
        reView.setAdapter(adapter);

        add_button = findViewById(R.id.add_button);
        add_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(mActivity, AddTripActivity.class);
                startActivity(intent);
            }
        });

        gv.setOnDateSelectedListener(new CalendarView.OnDateSelectedListener() {
            @Override
            public void onDateSelected(Calendar calendar, boolean isClick) {
                Log.e("选择了","选择了");

                choose_date = calendar.getYear()+"-"+calendar.getMonth()+"-"+calendar.getDay();
                schedule();
            }
        });

        schedule();

    }

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {

        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);

            switch (msg.what){

                case 1:

                    try {
                        lists.clear();
                        JSONObject object = new JSONObject(msg.obj.toString());

                        if (object.getInt("code") == 1){

                            JSONArray array = object.getJSONArray("data");
                            Gson gson = new Gson();
                            for (int i =0 ;i<array.length();i++){

                                Trip trip = gson.fromJson(array.get(i).toString(),Trip.class);
                                lists.add(trip);

                            }

                        }

                        adapter.setListAndNotifyDataSetChanged(lists);

                        if (lists.size()>0){
                            no_data.setVisibility(View.GONE);
                        }else{
                            no_data.setVisibility(View.VISIBLE);
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                    break;

            }

        }
    };

    SimpleDateFormat sdFormatter = new SimpleDateFormat("yyyy-MM-dd");
    public void schedule() {

        List<String> l = new ArrayList<>();
        l.add(choose_date + " 23:59:59");
        l.add(choose_date + " 00:00:00");
        //请求网络
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("sign", AllUtil.md5(sing(l)));
        params.put("token", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.token, ""));
        params.put("uid", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.uid, ""));
        params.put("ts", System.currentTimeMillis() / 1000); // 当前时间戳
        params.put("cmd", "schedule");
        params.put("data[starttime]", choose_date + " 00:00:00");
        params.put("data[endtime]", choose_date + " 23:59:59");
        OkHttp3ClientManager.getInstance().NetworkRequestMode("http://zmx.123xk.cn/doctor/", params, handler, 1, 404);

    }

}
